﻿using Microsoft.Xna.Framework;
using BruteForce.Animation;

namespace BruteForce.Screens.PlayingScreen.Map
{
    internal static class Camera
    {
        public static Vector2 location = Vector2.Zero;

        public static int ViewWidth { get; set; }
        public static int ViewHeight { get; set; }
        public static int WorldWidth { get; set; }
        public static int WorldHeight { get; set; }

        public static Vector2 DisplayOffset { get; set; }

        public static Vector2 Location
        {
            get { return location; }
            set
            {
                location = new Vector2(
                    MathHelper.Clamp(value.X, 0f, WorldWidth - ViewWidth),
                    MathHelper.Clamp(value.Y, 0f, WorldHeight - ViewHeight));
            }
        }

        public static Vector2 WorldToScreen(Vector2 worldPosition)
        {
            return worldPosition - Location + DisplayOffset;
        }

        public static Vector2 ScreenToWorld(Vector2 screenPosition)
        {
            return screenPosition + Location - DisplayOffset;
        }

        public static void Move(Vector2 offset)
        {
            Location += offset;
        }

        public static void focus(Vector2 position)
        {
            Vector2 prePos = new Vector2(position.X - ViewWidth / 2, position.Y - ViewHeight / 2);
            
            //alligning
            if (prePos.Y < 0) prePos.Y = 0;
            if (prePos.Y + ViewHeight > WorldHeight) prePos.Y = WorldHeight - ViewHeight;
            if (prePos.X < 0) prePos.X = 0;
            if (prePos.X + ViewWidth > WorldWidth) prePos.X = WorldWidth - ViewWidth;

            EaseInOutTransition transition = new EaseInOutTransition(0.5);
            var originalLocation = Location;
            var offsetX = prePos.X - Location.X;
            var offsetY = prePos.Y - Location.Y;
            transition.Callback = (t => Location = originalLocation + new Vector2((float) (offsetX*t.Value), (float)(offsetY*t.Value)));

            transition.StartTransition();
            //Location = prePos;
        }
    }
}